/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getUserInfo();
    var formData = {
        uid: uinfo.uid, 
        role_id: uinfo.role_id, 
        depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拼接字符串
function getHtml(e){
    var d = e.data, delay_str = e.delay_str;
    var h = '', i = 0, child, ii;
    for (; i < d.length; i ++) {
        h += '<div class="tbox">';
        h += '<table cellspacing="0">';
        h += '<tr>';
        h += '<td class="tdl">录入时间</td>';
        h += '<td class="tdr">' + d[i].in_day + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">项目名称</td>';
        h += '<td class="tdr">' + d[i].item_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">任务名称</td>';
        h += '<td class="tdr">' + d[i].task_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">总工时</td>';
        h += '<td class="tdr">' + d[i].delay + '</td>';
        h += '</tr>';
        if (d[i].status < 3) {
            h += '<tr>';
            h += '<td class="tdl"></td>';
            h += '<td class="tdr">';
            h += '<a href="javascript:;" onclick="edit(' + d[i].id + ');">修改</a>';
            h += '</td>';
            h += '</tr>';
        }
        h += '</table>';
        child = d[i].whList;
        for (ii = 0; ii < child.length; ii ++) {
            h += '<table class="child" cellspacing="0">';
            h += '<tr>';
            h += '<td class="tdl">工时项</td>';
            h += '<td class="tdr">' + child[ii].wh_title + '</td>';
            h += '</tr>';
            h += '<tr>';
            h += '<td class="tdl">工作内容</td>';
            h += '<td class="tdr">' + child[ii].title + '</td>';
            h += '</tr>';
            h += '<tr>';
            h += '<td class="tdl">开始时间</td>';
            h += '<td class="tdr">' + child[ii].start + '</td>';
            h += '</tr>';
            h += '<tr>';
            h += '<td class="tdl">结束时间</td>';
            h += '<td class="tdr">' + child[ii].end + '</td>';
            h += '</tr>';
            h += '<tr>';
            h += '<td class="tdl">时长</td>';
            h += '<td class="tdr">' + child[ii].delay + '</td>';
            h += '</tr>';
            h += '</table>';
        }
        h += '</div>';
    }
    $('#tbody').html(h);
    $('#foot').html('今日累计工时 ' + delay_str);
}


//编辑
function edit(v){
    window.location.href = 'whyw-edit.html?id=' + v;
}


//编辑保存
function modify(v){
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    var formData = {
        id: v, item_id: item_id,
        task_id: task_id,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//编辑弹窗
function wh_add(v){
    var uinfo = getUserInfo();
    var formData = {
        depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            res.data.wh_id = v;
            whAddHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//添加HTML
function whAddHtml(e){
    var h = '', i, wh_id = e.wh_id;;
    var whList = e.whList;
    var hourList = e.hour;
    var cntList = e.cntList;
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>工时项</div>';
    h += '<select id="config_whid" onchange="getCntList();">';
    h += '<option value="0">-选择-</option>';
    for (i = 0; i < whList.length; i ++) {
        h += '<option value="' + whList[i].id + '">';
        h += whList[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div>工时内容</div>';
    //h += '<input type="text" id="title" placeholder="输入名称"/>';
    h += '<select id="cnt_id">';
    h += '<option value="0">-选择内容-</option>';
    for (i = 0; i < cntList.length; i ++) {
        h += '<option value="' + cntList[i].id + '">';
        h += cntList[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<input type="hidden" id="wh_id" value="' + wh_id + '"/>';
    h += '<div>开始时间</div>';
    h += '<select id="start">';
    h += '<option value="0">-选择-</option>';
    for (i = 0; i < hourList.length; i ++) {
        h += '<option value="' + hourList[i] + '">';
        h += hourList[i];
        h += '</option>';
    }
    h += '</select>';
    h += '<div>结束时间</div>';
    h += '<select id="end">';
    h += '<option value="0">-选择-</option>';
    for (i = 0; i < hourList.length; i ++) {
        h += '<option value="' + hourList[i] + '">';
        h += hourList[i];
        h += '</option>';
    }
    h += '</select>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="whModify(0);">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//获取内容
function getCntList(){
    var formData = {
        wh_id: $('#config_whid').val(),
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/cnt-get',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var cntList = res.data;
            var h = '', i;
            if (cnt_id == 0) {
                h += '<option value="0" selected>';
            } else {
                h += '<option value="0">';
            }
            h += '-选择内容-';
            h += '</option>';
            for (i = 0; i < cntList.length; i ++) {
                if (cnt_id == cntList[i].id) {
                    h += '<option value="' + cntList[i].id + '" selected>';
                } else {
                    h += '<option value="' + cntList[i].id + '">';
                }
                h += cntList[i].title;
                h += '</option>';
            }
            $('#cnt_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//编辑弹窗
function wh_edit(v){
    var uinfo = getUserInfo();
    var formData = {
        id: v, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            whEditHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//详情HTML
function whEditHtml(e){
    var d = e.data, i;
    var hourList = e.hour;
    var cntList = e.cntList;
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>工时项</div>';
    h += '<input type="text" value="' + d.config_title + '" disabled/>';
    h += '<input type="hidden" id="config_whid" value="' + d.config_whid + '"/>';
    h += '<input type="hidden" id="wh_id" value="' + d.wh_id + '"/>';
    h += '<div>工作内容</div>';
    //h += '<input type="text" id="title" placeholder="输入名称" value="' + d.title + '"/>';
    h += '<select id="cnt_id">';
    if (d.cnt_id == 0) {
        h += '<option value="0" selected>-选择内容-</option>';
    } else {
        h += '<option value="0">-选择内容-</option>';
    }
    for (i = 0; i < cntList.length; i ++) {
        if (d.cnt_id == cntList[i].id) {
            h += '<option value="' + cntList[i].id + '" selected>';
        } else {
            h += '<option value="' + cntList[i].id + '">';
        }
        h += cntList[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div>开始时间</div>';
    h += '<select id="start">';
    if (d.starthour == 0) {
        h += '<option value="0" selected>-选择-</option>';
    } else {
        h += '<option value="0">-选择-</option>';
    }
    for (i = 0; i < hourList.length; i ++) {
        if (d.starthour == hourList[i]) {
            h += '<option value="' + hourList[i] + '" selected>';
        } else {
            h += '<option value="' + hourList[i] + '">';
        }
        h += hourList[i];
        h += '</option>';
    }
    h += '</select>';
    h += '<div>结束时间</div>';
    h += '<select id="end">';
    if (d.endhour == 0) {
        h += '<option value="0" selected>-选择-</option>';
    } else {
        h += '<option value="0">-选择-</option>';
    }
    for (i = 0; i < hourList.length; i ++) {
        if (d.endhour == hourList[i]) {
            h += '<option value="' + hourList[i] + '" selected>';
        } else {
            h += '<option value="' + hourList[i] + '">';
        }
        h += hourList[i];
        h += '</option>';
    }
    h += '</select>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="whModify(' + d.id + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//编辑保存
function whModify(v){
    var uinfo = getUserInfo();
    var config_whid = $('#config_whid').val();
    var wh_id = $('#wh_id').val();
    var cnt_id = $('#cnt_id').val();
    var title = $('#title').val();
    var start = $('#start').val();
    var end = $('#end').val();
    var formData = {
        id: v, uid: uinfo.uid, depart_id: uinfo.depart_id,
        config_whid: config_whid, wh_id: wh_id, cnt_id: cnt_id,
        title: title, start: start, end: end,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function whDel(v){
    var formData = {
        id: v,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//审核列表
function authList(){
    var uinfo = getUserInfo();
    var formData = {
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/auth-list',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            authHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拼接字符串
function authHtml(d){
    var h = '', i = 0;
    for (; i < d.length; i ++) {
        h += '<table class="tbox" cellspacing="0">';
        h += '<tr>';
        h += '<td class="tdl">项目名称</td>';
        h += '<td class="tdr">' + d[i].item_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">任务名称</td>';
        h += '<td class="tdr">' + d[i].task_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">工时名称</td>';
        h += '<td class="tdr">' + d[i].wh_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">开始时间</td>';
        h += '<td class="tdr">' + d[i].starthour + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">结束时间</td>';
        h += '<td class="tdr">' + d[i].endhour + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">时长</td>';
        h += '<td class="tdr">' + d[i].delay + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl"></td>';
        h += '<td class="tdr">';
        h += '<a href="javascript:;" style="color: red;" onclick="auth_refuse(' + d[i].id + ');">拒绝</a>';
        h += '&nbsp; <a href="javascript:;" onclick="auth_ok(' + d[i].id + ');">通过</a>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#tbody').html(h);
}


//拒绝弹窗
function auth_refuse(v){
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>拒绝原因</div>';
    h += '<textarea id="remark"></textarea>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="auth_no(' + v + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//拒绝
function auth_no(v){
    var formData = {
        id: v, status: 1, remark: $('remark').val(),
    };
    if (formData.remark == '') {
        msgPop({msg: '备注必须的'}); return
    }
    auth_api(formData);
}


//通过
function auth_ok(v){
    var formData = {
        id: v, status: 2, remark: '',
    };
    auth_api(formData);
}


//审核API
function auth_api(e){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/auth-set',
        data: e,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}